<%@ page import="com.the8thfold.anton.ant.AntJobStatus; com.the8thfold.anton.users.Roles; org.codehaus.groovy.grails.plugins.springsecurity.SpringSecurityUtils; com.the8thfold.anton.ant.JobStatus; org.apache.tools.ant.Target" contentType="text/html;charset=UTF-8" %>
<html>
<head>
    <title>View Run</title>
    <meta name="layout" content="main" />
    <link rel="stylesheet" href="${resource(dir:'js/codemirror',file:'codemirror.css')}" />
    <link rel="stylesheet" href="${resource(dir:'js/codemirror/mode/xml',file:'xml.css')}" />

    <g:javascript library="codemirror/overlay" />
    <g:javascript library="codemirror/codemirror" />
    <g:javascript library="codemirror/mode/xml/xml" />
    <g:javascript>
        $(document).ready(function(){

            $("#tabs").tabs({
                ajaxOptions: {
                    error: function( xhr, status, index, anchor ) {
                        $( anchor.hash ).html(
                        "Error" );
                    }
                }
            });
        });
    </g:javascript>
</head>
<body>

<div>
    <table style="border: none;">
        <tr>
            <td width="1%"><h1>Run:</h1></td>
            <td>
                <g:if test="${SpringSecurityUtils.ifAllGranted(Roles.ROLE_RESULTS_ADMIN.toString())}">
                    <g:formRemote onSuccess="jQuery('#${result.runResultsId}_name').effect('highlight',{color: '#90ee90'},1000);" name="f2" url="[controller: 'result', action: 'updateField', id: result.runResultsId]">
                        <g:hiddenField name="field" value="name"/>
                        <input id="${result.runResultsId}_name" class="ui-corner-all" type="text" name="value" value="${result.name}" style="width: 50%;"/>
                        <a href="javascript: void();" onclick="jQuery('#f2').submit();" name="submit" value="" href="#" class="btn save" icon="ui-icon-disk"></a>
                    </g:formRemote>
                </g:if>
                <g:else>${result.name}</g:else>
            </td>
        </tr>
    </table>
    <hr/>
    <br/>
    <g:if test="${error}">
        <div class="errors">${error}</div>
        <br/>
    </g:if>
    <span><b>State:</b> ${result.status}</span>
    <g:if test="${AntJobStatus.RUNNING.equals(result.status)}">
        <g:javascript>
            setTimeout(function(){
                document.location.reload(true);
            },5000);
        </g:javascript>
    </g:if>

    <div class="css_right toolbar">
        <button ${result.isCreated() && result.job.isActive() ?: 'disabled=true'} ${SpringSecurityUtils.ifAllGranted(Roles.ROLE_JOB_RUN.toString()) ?: 'disabled'} class="btn-text" onclick="document.location.href='${createLink(controller: 'result', action: 'run', id: result.runResultsId )}'">Run</button>
        <button ${result.isRunning() ?: 'disabled=true'} class="btn-text" ${SpringSecurityUtils.ifAllGranted(Roles.ROLE_JOB_RUN.toString()) ?: 'disabled'} onclick="document.location.href='${createLink(controller: 'result', action: 'stop', id: result.runResultsId )}'">Stop</button>
        <button ${!result.isRunning() ?: 'disabled=true'} class="btn-text" ${SpringSecurityUtils.ifAllGranted(Roles.ROLE_RESULTS_ADMIN.toString()) ?: 'disabled'} onclick="if (confirm('Are you sure?')){document.location.href='${createLink(controller: 'result', action: 'delete', id: result.runResultsId )}';}">Delete</button>
    </div>
    <br/>

    <h2>Description</h2>
    <br/>
    <table>
        <tr>
            <th>Job</th>
            <th>Script</th>
            <th>Created By</th>
            <th>Run By</th>
        </tr>
        <tr>
            <td >
                <g:if test="${SpringSecurityUtils.ifAllGranted(Roles.ROLE_JOB_RUN.toString())}">
                    <g:link controller="job" action="view" id="${result.job.jobId}">${result.job.name}</g:link>
                </g:if>
                <g:else>${result.job.name}</g:else>
            </td>
            <td >
                <g:if test="${SpringSecurityUtils.ifAllGranted(Roles.ROLE_SCRIPT_ADMIN.toString())}">
                    <g:link controller="script" action="view" id="${result.job.script.scriptId}">${result.job.script.description}</g:link>
                </g:if>
                <g:else>${result.job.script.description}</g:else>
            </td>
            <td>${result.createdBy.firstName + " " + result.createdBy.lastName} (<prettytime:display date="${result.dateCreated}" />)</td>
            <td>
                <g:if test="${result.runBy}">
                    ${result.runBy?.firstName + " " + result.runBy?.lastName} (<prettytime:display date="${result.dateRun}" />)
                </g:if>
            </td>
        </tr>
    </table>
    <br/>
    <h2>Log
        <div class="css_left toolbar">
            <a class="btn" icon="ui-icon-refresh" href="${createLink(controller: 'result', action: 'view', id: result.runResultsId)}"></a>
        </div>
        <div class="css_right toolbar">
            <input type="checkbox" class="btn" checked="on" value="Auto Refresh"/>
        </div>
    </h2>
    <div id="tabs">
        <ul>
            <li><a href="${createLink(controller: 'result', action: 'viewLog', id: result.runResultsId)}">Run Log</a></li>
            <li><a href="${createLink(controller: 'result', action: 'viewErrorLog', id: result.runResultsId)}">Run Error Log</a></li>
        </ul>
    </div>
    <br/><br/><br/>
</div>

</body>
</html>